using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Northwoods.Go;
using Northwoods.Go.Xml;
using Workflows.Components;

namespace Workflows.Diagrams
{
    [Serializable]
    public class TransformFlowLink:GoXmlTransformer 
    {
        public TransformFlowLink()
        {
            this.TransformerType = typeof(FlowLink);
            this.ElementName = "flow";
        }

        public override void GenerateAttributes(object obj)
        {
            base.GenerateAttributes(obj);
            FlowLink flow = (FlowLink)obj;
            GoPort p = (GoPort)flow.FromPort;
            if (p != null)
            {
                string fromId = this.Writer.FindShared(p);
                this.WriteAttrVal("from", fromId);
            }
            p = (GoPort)flow.ToPort;
            if (p != null)
            {
                string toId = this.Writer.FindShared(p);
                this.WriteAttrVal("to", toId);
            }
            this.WriteAttrVal("points", flow.RealLink.CopyPointsArray());
            LinkLabel label = flow.Label;
            if (label != null)
            {
                this.WriteAttrVal("label", label.Text);
                if (!label.Offset.Equals(new SizeF(-999999, -999999)))
                {
                    this.WriteAttrVal("labelOffset", label.Offset);
                }
                this.WriteAttrVal("labelSegment", label.Segment);
                this.WriteAttrVal("labelPercentage", label.SegmentPercentage);
            }
            
            if ((flow != null) && (flow.FromPort != null))
            {
                PortDirection dataLinkType = (PortDirection)flow.FromPort.UserFlags;
                this.WriteAttrVal("wfDataLinkType", dataLinkType.ToString());
            }
        }

        public override void ConsumeAttributes(object obj)
        {
            base.ConsumeAttributes(obj);
            FlowLink flow = (FlowLink)obj;
            string fromId = StringAttr("from", null);
            if (!string.IsNullOrEmpty(fromId))
            {
                GoPort fromPort = (GoPort)this.Reader.FindShared(fromId);
                flow.FromPort = fromPort;
            }
            string toId = StringAttr("to", null);
            if (!string.IsNullOrEmpty(toId))
            {
                GoPort toPort = (GoPort)this.Reader.FindShared(toId);
                flow.ToPort = toPort;
            }
            if (this.IsAttrPresent("points"))
                flow.RealLink.SetPoints(PointFArrayAttr("points", null));
            LinkLabel label = flow.Label;
            if (this.IsAttrPresent("label") && (label != null))
            {
                label.Text = StringAttr("label", label.Text);
                if (this.IsAttrPresent("labelOffset"))
                    label.Offset = SizeFAttr("labelOffset", label.Offset);
                label.Segment = Int32Attr("labelSegment", label.Segment);
                label.SegmentPercentage = SingleAttr("labelPercentage", 50);
                flow.FromPort.UserFlags = Int32Attr("wfDataLinkType", (int)PortDirection.Outbound);
                PortDirection dataLinkType = (PortDirection)flow.FromPort.UserFlags;
                switch (dataLinkType)
                {
                    case PortDirection.Outbound:
                        flow.Pen = new Pen(Color.Blue, 2);
                        break;
                    case PortDirection.Error:
                        flow.Pen = new Pen(Color.Red, 2);
                        break;
                    default:
                        break;
                }
                flow.LayoutChildren(null);
            }
        }
    }
}
